// pages/personForCar/personForCar.js
var app = getApp();
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    userInfo: '',
    imageUrl: app.globalData.imageUrl,
    bottabData: app.globalData.bottabDataForSfc,
    tabData: [{ 'txt': '我的关注', current: true }, { 'txt': '我的发布', current: false }, { 'txt': '待付款', current: false },{ 'txt': '待收款', current: false }],
    actiItem0: [],
    actiItem1: [],
    actiItem2: [],
    actiItem3: [],
    isFollow:true,
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: true,
    hasData3: true,
    hasData1:true,
    hasData2: true,
    hasData0: true,
    page1: 1,
    page2:1,
    page0:1,
    page3: 1,
    current:0,
    qrcode:true,
    currentData:{},
    codeImg:'',
    showSelect:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var obj = { '1': '车找人', '2': '人找车', '3': '货找车', '4': '车找货' };
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['bottabData[2].current']: true
    })
    
  },
  enterPerson: function (e) {
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id=' + e.target.id
    })
  },
  showCallPhone: function (e) {
    this.data.currentData = e.currentTarget.dataset.item;
    if (this.data.currentData.contactNum) {
      this.setData({
        currentData: this.data.currentData,
        showSelect: true
      })
    }
  },
  callPhone:function(e){
    wx.makePhoneCall({
      phoneNumber: this.data.currentData.contactNum //仅为示例，并非真实的电话号码
    })
    this.setData({
      showSelect: false
    })
  },
  cancelPhone:function(e){
    this.setData({
      showSelect: false
    })
  },
  showDetail: function (e) {
    var isblankout = e.currentTarget.dataset.value == undefined ? false : true;
    var data = e.currentTarget.dataset.item;
    if (this.data.current == 2){
      data = e.currentTarget.dataset.item.freeRideInfo;
      if (data.userId == this.data.userInfo.id) {
        // if (data.type==4||data.type==1){
        //   data.contactNum = e.currentTarget.dataset.item.guestInfo.phone;
        // }else{
          data.contactNum = e.currentTarget.dataset.item.masterInfo.phone;
        // }
      }
    } else if (this.data.current == 3){
      data = e.currentTarget.dataset.item.freeRideInfo;
      if (data.userId == this.data.userInfo.id) {
        data.contactNum = e.currentTarget.dataset.item.guestInfo.phone;
      }
    }
    if (this.data.current == 2 || this.data.current == 3) {
      wx.navigateTo({
        url: '/pages/carDetail/carDetail?isMine=true&isOrder=true&id=' + data.id + '&contactNum='+ data.contactNum
      })
    } else {
      wx.navigateTo({
        url: '/pages/carDetail/carDetail?isMine=true&id=' + data.id
      })
    }
  },
  payment:function(e){//付款
    var that=this;
    this.setData({
      currentData: e.currentTarget.dataset.item
    });
    var item = e.currentTarget.dataset.item;
    if (item.type==1||item.type==4){
      wx.request({
      url: app.globalData.httpApiCar + 'freeRidePay/surplusTravelExpensesPayment', //仅为示例，并非真实的接口地址
      data: {
        openId: app.globalData.openId,
        type: 1,
        backupA: item.backupF,
        backupB: item.id,
        backupC: item.type,
        backupD: 4,
        adds: 2,
        userId: this.data.userInfo.id,
        body: '顺风车支付剩余路费'
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          that.requestPayment(res.data.data);
        } else {
          that.showTipModal(res.data.error);
        }
      }
    })
    }else{
      wx.navigateTo({
        url: "/pages/recharge/recharge?page=1&id="+item.id,
      })
    }
  },
  gathering: function (e) {//收款
    var that = this;
    this.setData({
      currentData: e.currentTarget.dataset.item
    });
    var item = e.currentTarget.dataset.item;
    wx.request({
      url: app.globalData.httpApiCar + 'freeRidePay/driverConfirmedTheReceipt', //仅为示例，并非真实的接口地址
      data: {
        id: item.id,
        // masterStatus:2
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          that.showTipModal('收款成功');
          that.getGathering(true);
        } else {
          that.showTipModal(res.data.error);
        }
      }
    })
  },
  showTipModal:function(title){
    wx.showModal({
      title: '提示',
      content: title,
      showCancel: false,
      success: function (res) {

      }
    })
  },
  requestPayment: function (obj) {
    var that = this;
    wx.requestPayment({
      'timeStamp': obj.timeStamp,
      'nonceStr': obj.nonceStr,
      'package': obj.package,
      'signType': obj.signType,
      'paySign': obj.paySign,
      'success': function (res) {
        wx.showModal({
          title: '提示',
          content: '支付成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              that.getPaymentList(true);
            }
          }
        })
      },
      'fail': function (res) {                                                                                                                        
        that.showTipModal('支付失败');
      }
    })
  },
  getSeachData(flag) {//获取我的关注
    var _this = this;
    var obj = { '车找人': 1, '人找车': 2, '货找车': 3, '车找货': 4 };
    wx.request({
      url: app.globalData.httpApiCar + 'freeRideInfo/myAttention', //仅为示例，并非真实的接口地址
      data: {
        userId: this.data.userInfo.id,
        currentPage: this.data.page0,
        pageNumber: '10'
      },
      success: function (res) {
        if (res.data.data.pp.length < 10) {
          _this.setData({
            hasData0: false
          })
        } else {
          _this.setData({
            hasData0: true
          })
        }
        res.data.data.pp.forEach(function (ele) {
          ele.type = obj[ele.typeName];
          if (ele.createTime) {
            ele.time = util.getDays(ele.createTime)
          }
          if (ele.labelArray != undefined && ele.labelArray != '') {
            ele.mark = ele.labelArray.split('/');
          } else {
            ele.mark = [];
          }
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem0: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.actiItem0 = _this.data.actiItem0.concat(res.data.data.pp);
        _this.setData({
          actiItem0: _this.data.actiItem0
        })
      }
    })
  },
  getPushList(flag) {//获取我的发布
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'freeRideInfo/page', //仅为示例，并非真实的接口地址
      data: {
        status: 1,
        userId: this.data.userInfo.id,
        currentPage: this.data.page1,
        pageNumber: '10'
      },
      // method: 'POST',
      // header: {
      //   'content-type': 'application/x-www-form-urlencoded' // 默认值
      // },
      success: function (res) {
        if (res.data.data.pp.length < 10) {
          _this.setData({
            hasData1: false
          })
        } else {
          _this.setData({
            hasData1: true
          })
        }
        res.data.data.pp.forEach(function (ele) {
          ele.isStart = false;//未出发
          if (new Date().getTime() > new Date(ele.backupA.replace(/-/g, "/")).getTime()) {
            ele.isStart = true;
          }
          if (ele.createTime) {
            ele.time = util.getDays(ele.createTime)
          }
          if (ele.labelArray != undefined && ele.labelArray != '') {
            ele.mark = ele.labelArray.split('/');
          } else {
            ele.mark = [];
          }
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem1: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.actiItem1 = _this.data.actiItem1.concat(res.data.data.pp);
        _this.setData({
          actiItem1: _this.data.actiItem1
        })
      }
    })
  },
  getPaymentList(flag) {//获取待付款列表?masterId=**&masterStatus=1&currentPage=1&pageNumber=20
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'freeRidePay/page', //仅为示例，并非真实的接口地址
      data: {
        guestId: this.data.userInfo.id,
        currentPage: this.data.page2,
        pageNumber: '10',
        backupC:0,
        backupB: 0,
        guestStatus: 1,
        status: 1
      },
      success: function (res) {
        if (res.data.data.pp.length < 10) {
          _this.setData({
            hasData2: false
          })
        } else {
          _this.setData({
            hasData2: true
          })
        }
        res.data.data.pp.forEach(function (ele) {
          if (ele.freeRideInfo.labelArray != undefined && ele.freeRideInfo.labelArray != '') {
            ele.freeRideInfo.mark = ele.freeRideInfo.labelArray.split('/');
          } else {
            ele.freeRideInfo.mark = [];
          }
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem2: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.actiItem2 = _this.data.actiItem2.concat(res.data.data.pp);
        _this.setData({
          actiItem2: _this.data.actiItem2
        })
      }
    })
  },
  getGathering(flag) {//获取待收款列表
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'freeRidePay/page', //仅为示例，并非真实的接口地址
      data: {
        masterId: this.data.userInfo.id,
        currentPage: this.data.page2,
        pageNumber: '10',
        backupC: 0,
        backupB: 0,
        masterStatus: 1,
        status: 1
      },
      success: function (res) {
        if (res.data.data.pp.length < 10) {
          _this.setData({
            hasData3: false
          })
        } else {
          _this.setData({
            hasData3: true
          })
        }
        res.data.data.pp.forEach(function (ele) {
          if (ele.freeRideInfo.labelArray != undefined && ele.freeRideInfo.labelArray != '') {
            ele.freeRideInfo.mark = ele.freeRideInfo.labelArray.split('/');
          } else {
            ele.freeRideInfo.mark = [];
          }
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem3: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.actiItem3 = _this.data.actiItem3.concat(res.data.data.pp);
        _this.setData({
          actiItem3: _this.data.actiItem3
        })
      }
    })
  },
  tanSwitch:function(e){
    var index = parseInt(e.target.dataset.value);
    this.data.tabData.forEach(function(ele){
      ele.current=false;
    })
    this.data.tabData[index].current=true;
    
    this.setData({
      tabData: this.data.tabData,
      current: index
    })
  },
  /**
   * 
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getSeachData(true);
    this.getPushList(true);
    this.getPaymentList(true);
    this.getGathering(true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      ['page' + this.data.current]: 1
    })
    if (this.data.current==0){
      this.getSeachData(true);
    } else if (this.data.current == 1) {
      this.getPushList(true);
    } else if (this.data.current == 2) {
      this.getPaymentList(true);
    }else{
      this.getGathering(true);
    } 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data['hasData'+this.data.current]) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        ['page' + this.data.current]: this.data['page' + this.data.current] + 1
      });
      if (this.data.current == 0) {
        this.getSeachData();
      } else if (this.data.current == 1) {
        this.getPushList();
      } else if (this.data.current == 2) {
        this.getPaymentList();
      } else {
        this.getGathering();
      } 
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})